<?php

/**
 * MySQL database driver class.
 * Implement DBDriver class.  
 *  
 * @package SpotLight.core.db.divers
 * @see SpotLight.core.db.DBDriver
 * @version since begin
 * @author asmhack
 * @license GPL
 * @copyright Serhiy Zaporozhets'
 */
class MySQLDriver extends DBDriver {
	
	public function connect($dbConfHost, $dbConfLogin, $dbConfPasswd, $dbConfDB, $encoding) {
		
		$this->link = mysql_connect ( $dbConfHost, $dbConfLogin, $dbConfPasswd );
		if ($this->link == null)
			throw new DBException ( 'Can\'t connect to DataBase. Please check  connection data in "SpotLight/configs/<strong>dbConfig.php</strong>" file!' );
		$res = mysql_select_db ( $dbConfDB, $this->link );
		if ($res == null)
			throw new DBException ( 'Can\'t use DataBase! Please check connection data in "SpotLight/configs/<strong>dbConfig.php</strong>" file!<br/>' . mysql_error ( $this->link ) );
		$query = "set charset $encoding;";
		$res = mysql_query ( $query, $this->link );
		if ($res == null)
			throw new DBException ( 'Can\'t set charset! Please check connection data in "SpotLight/configs/<strong>dbConfig.php</strong>" file!' );
		$query = "set names $encoding;";
		$res = mysql_query ( $query, $this->link );
		if ($res == null)
			throw new DBException ( 'Can\'t set names! Please check connection data in "SpotLight/configs/<strong>dbConfig.php</strong>" file!' );
	}
	
	public function query($query) {
		
		$this->res = mysql_query ( $query, $this->link );
		if ($this->res != false)
			return $this->res;
		else {
			throw new DBException ( 'Could not submit query.' );
		}
	}
	
	public function error() {
		return mysql_query ( $this->link );
	}
	
	public function getQueryResult() {
		return $this->res;
	}
	
	public function escapeString($str) {
		return mysql_real_escape_string ( $str, $this->link );
	}
	
	public function fetch_assoc() {
		return mysql_fetch_assoc ( $this->res );
	}
	
	public function fetch_object() {
		return mysql_fetch_object ( $this->res );
	}
	
	public function fetch_all() {
		$ret = array ();
		while ( $row = $this->fetch_assoc () )
			$ret [] = $row;
		return $ret;
	}
	
	public function affected_rows() {
		return mysql_affected_rows ( $this->link );
	}
	
	public function num_rows() {
		return mysql_num_rows ( $this->res );
	}
	
	public function close() {
		if (is_resource ( $this->link )) {
			$res = mysql_close ( $this->link );
			/*if ($res == null)
				throw new DBException ( 'Can\'t close connection.' );*/
		}
		return $res;
	}
	
	public function getLink() {
		return $this->link;
	}
	
	public function select_db($dbName) {
		$res = mysql_select_db ( $dbName, $this->link ); // or die('Can\'t use DataBase. Please check connection data in config file!'.mysql_error($this->link));
		if ($res == null)
			throw new DBException ( 'Can\'t use DataBase! Wrong Database name.' );
	}
	
	public function last_id() {
		return mysql_insert_id ( $this->link );
	}
}

?>